//
//  StatusViewModel.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 16/1/11.
//  Copyright © 2016年 张鹏飞. All rights reserved.
//

import UIKit

class StatusViewModel: CustomStringConvertible {
    
/// 微博模型
    var status: Status
    
    /// 缓存的行高
    lazy var rowHeight: CGFloat = {
        
        // 1. cell
        let cell = StatusRetweetedCell(style: .Default, reuseIdentifier: StatusCellRetweetedId)
        
        // 2. 记录高度
        return cell.rowHeight(self)
        }()
    
    /// 用户头像
    var userProfileUrl: NSURL {
    
        return NSURL(string: status.user?.profile_image_url ?? "")!
    }
    /// 用户默认头像
    var userDefaultIconView: UIImage {
        return UIImage(named: "avatar_default_big")!
    }
    /// 用户会员图标
    var userMemberImage: UIImage? {
        
        // 根据 mbrank 来生成图像
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    /// 用户认证图标
    var userVipImage: UIImage? {
    
        switch(status.user?.verified_type ?? -1) {
        
        case 0: return UIImage(named: "avatar_vip")
        case 2,3,5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
    
    /// 缩略图 URL 数组 - 存储型属性
    var thumbnailUrls: [NSURL]?
    
    /// 构造函数
    init(status: Status) {
    
        self.status = status
        
        //根据模型生成缩略图的数组
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
    
            //创建缩略图数组
            thumbnailUrls = [NSURL]()
            
            //遍历字典数组
            for dict in urls {
            
                let url = NSURL(string: dict["thumbnail_pic"]!)
                
                thumbnailUrls?.append(url!)
            }
        }
    }
/// 描述信息
    var description: String {
    
        return status.description + "配图数组 \(thumbnailUrls ?? [] as NSArray)"
    }
}